﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Wsitech.BLL;
using Wsitech.Utility;
using Wsitech.Model;
public partial class Backend_TextInfoAttach : BackendPageBase
{
    //新闻图片
    private string imageDir = "/UploadFile/Image/";
    private int image_width = 680;
    private int image_height = 300;
    private int thumb_image_width = 100;
    private int thumb_image_height = 44;

    protected string tid = HttpContext.Current.Request["tid"] ?? "";
    protected string attachid = HttpContext.Current.Request["attachid"] ?? "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (attachid != "")
            {
                initAttach(attachid);
            }
        }
    }

    //初始化信息
    private void initAttach(string attachid)
    {
        AttachInfo attach = AttachInfoBLL.Instance.GetDetail(attachid);
        if (attach != null)
        {
            txtAttachDesc.Text = attach.Description;

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (CheckParams())
        {
            if (TextInfoBLL.Instance.GetDetail(tid) != null)
            {
                if (hfAttachPath.Value != "")
                {
                    string filepath = Server.MapPath(hfAttachPath.Value.Trim());
                    if (File.Exists(filepath))
                    {
                        string uniqueFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + Path.GetExtension(filepath);
                        //新闻图片
                        string filename = imageDir + uniqueFileName;
                        string imgPath = Server.MapPath(filename);
                        string thumbPath = AttachInfoBLL.Instance.GetThumbPath(imgPath);

                        ImageProcess.ImageToProperSize(filepath, image_width, image_height, imgPath);
                        ImageProcess.ImageToProperSize(filepath, thumb_image_width, thumb_image_height, thumbPath);

                        AttachInfoBLL.Instance.AddAttach(tid, txtAttachDesc.Text.Trim(), UtilityEnum.AttachType.Image, filename);

                        File.Delete(filepath);
                    }
                    else
                    {
                        base.Alert("图片不存在！");
                        return;
                    }
                }
            }
        }
        base.RefreshParent();
    }

    //数据验证
    private bool CheckParams()
    {
        if (txtAttachDesc.Text.Trim() == "")
        {
            base.Alert("请输入图片描述");
            return false;
        }
        else
        {
            if (txtAttachDesc.Text.Trim().Length > 100)
            {
                base.Alert("图片描述不超过100字");
                return false;
            }
        }
        if (hfAttachPath.Value == "")
        {
            base.Alert("请上传图片");
            return false;
        }
        return true;
    }
}